package com.yonyou.pmclouds.messagenotice.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertMultiProcessor;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.SqlUtil;
import com.yonyou.pmclouds.messagenotice.constructor.impl.MessageNoticeConstructor;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.messagenotice.mapper.MessageNoticeMapper;
import com.yonyou.pmclouds.messagenotice.rmiitf.MessageNoticeMaintain;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = MessageNoticeMaintain.class)
public class MessageNoticeMaintainImpl implements MessageNoticeMaintain {

    private final MessageNoticeConstructor noticeConstructor;

    private final MessageNoticeMapper noticeMapper;

    @Autowired
    public MessageNoticeMaintainImpl(MessageNoticeConstructor noticeConstructor,
                                     MessageNoticeMapper noticeMapper) {
        this.noticeConstructor = noticeConstructor;
        this.noticeMapper = noticeMapper;
    }


    @Override
    public MessageNoticeVO[] insertMessageNotice(Entity vo) throws BusinessException {
        MessageNoticeVO[] noticeVOs = noticeConstructor.generateNotice(vo);
        if(noticeVOs == null || noticeVOs.length <= 0) {
            return null;
        }

        MultiInsertBpTemplate<MessageNoticeVO> insertBpTemplate = new MultiInsertBpTemplate<>(noticeMapper);
        insertBpTemplate.addBeforeProcessor(new AuditInfoInsertMultiProcessor<MessageNoticeVO>());

        return insertBpTemplate.insert(noticeVOs);
    }

    @Override
    public MessageNoticeVO[] insertBatchMessageNotice(Entity[] entities) throws BusinessException {
        if(entities == null || entities.length <= 0 ){
            return null;
        }
        List<MessageNoticeVO> messageNoticeVOList = new ArrayList<>();
        for(Entity entity : entities){
            MessageNoticeVO[] noticeVOs = noticeConstructor.generateNotice(entity);
            if(noticeVOs != null && noticeVOs.length > 0){
                messageNoticeVOList.addAll(Arrays.asList(noticeVOs));
            }
        }
        if(messageNoticeVOList.size() > 0){
            MultiInsertBpTemplate<MessageNoticeVO> insertBpTemplate = new MultiInsertBpTemplate<>(noticeMapper);
            insertBpTemplate.addBeforeProcessor(new AuditInfoInsertMultiProcessor<MessageNoticeVO>());
            return insertBpTemplate.insert(messageNoticeVOList.toArray(new MessageNoticeVO[]{}));
        }
        return null;

    }

    @Override
    public MessageNoticeVO[] deleteNotices(MessageNoticeVO[] noticeVOS) throws BusinessException {
        SingleDeleteBpTemplate<MessageNoticeVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(noticeMapper);
        for(MessageNoticeVO noticeVO : noticeVOS) {
            deleteBpTemplate.delete(noticeVO);
        }
        return noticeVOS;
    }

    @Override
    public boolean updateNoticeRead(String[] noticeIds) throws BusinessException {
        if(ArrayUtils.isEmpty(noticeIds)) return false;
        noticeMapper.updateNoticeRead(SqlUtil.getSqlIn(noticeIds));
        return true;
    }
}
